package com.ece.core.editors;

import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.internal.ui.JavaPlugin;
import org.eclipse.jdt.internal.ui.javaeditor.IClassFileEditorInput;
import org.eclipse.jdt.internal.ui.javaeditor.InternalClassFileEditorInput;
import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor;

public class JavaReviewEditor extends JavaEditor {

    public JavaReviewEditor() {
	setDocumentProvider(JavaPlugin.getDefault()
		.getCompilationUnitDocumentProvider());
	setEditorContextMenuId("#ClassFileEditorContext"); //$NON-NLS-1$
	setRulerContextMenuId("#ClassFileRulerContext"); //$NON-NLS-1$
	setOutlinerContextMenuId("#ClassFileOutlinerContext"); //$NON-NLS-1$
	setKeyBindingScopes(new String[] { "com.ece.editor.review" });
    }

    @Override
    public boolean isEditable() {
	return false;
    }

    @Override
    protected IJavaElement getElementAt(int offset) {

	if (getEditorInput() instanceof InternalClassFileEditorInput) {
	    try {
		InternalClassFileEditorInput input = (InternalClassFileEditorInput) getEditorInput();
		return input.getClassFile().getElementAt(offset);
	    } catch (JavaModelException x) {
	    }
	}
	return null;
    }

    @Override
    protected IJavaElement getCorrespondingElement(IJavaElement element) {
	if (getEditorInput() instanceof IClassFileEditorInput) {
	    IClassFileEditorInput input = (IClassFileEditorInput) getEditorInput();
	    IJavaElement parent = element.getAncestor(IJavaElement.CLASS_FILE);
	    if (input.getClassFile().equals(parent))
		return element;
	}
	return null;
    }

}
